# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author : Lawrence
# @File : concat_pdf.py
# @Project : worktool
# @Time : 2024/11/11 18:26


import os
from PyPDF2 import PdfReader, PdfWriter

def merge_pdfs(paths, output):
    pdf_writer = PdfWriter()

    for path in paths:
        pdf_reader = PdfReader(path)
        for page in range(len(pdf_reader.pages)):
            # 将每页添加到PdfFileWriter对象中
            # pdf_writer.addPage(pdf_reader.getPage(page))
            pdf_writer.add_page(pdf_reader.pages[page])

    # 写入到输出PDF文件
    with open(output, 'wb') as out:
        pdf_writer.write(out)

if __name__ == '__main__':
    # 要合并的PDF文件路径列表
    paths = ['./data/2024-JLDJCA-F4005某单位办公助手-AI辅助生成系统(1)(1).pdf', './data/merged.pdf']  # 请替换为您的PDF文件名
    # 输出文件的名称
    output = 'merged.pdf'

    # 调用函数合并PDF
    merge_pdfs(paths, output)
